<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * 用户中心用户相关的东西
 *
 * @author eoe2005
 */
class UserModel {
    /**
     *判断邮箱是否已经被注册
     * @param type $mail
     */
    public function hasMail($mail){
        $db = getDB();
        $rs = $db->get_row("SELECT Femail FROM t_user WHERE Femail='%s'",$mail);
        if($rs){
            return true;
        }else{
            return false;
        }
    }
    /**
     *判断邮箱是否注册了3天还未验证
     * @param type $mail
     * @return boolean  true的时候是不能注册的
     */
    public function regNotVerify3Day($mail){
        $db = getDB();
        $row = $db->get_var(sprintf("SELECT cunt(Fuid) FROM t_user_reg WHERE Femail='%s' AND Fregtime>%d",$mail,time() - 86400*3));
        if($row){
            return true;
        }
        return false;
    }
    /**
     *更具uid得到用户的注册信息 ,只有在沿着账号的时候使用
     * @param type $uid
     * @return type 
     */
    public function getRegUser($uid){
        $db = getDB();
        return $db->get_row(sprintf('SELECT * FROM t_user_reg WHERE Fuid=%d',$uid));
    }
    /**
     *注册新用户
     * @param type $fname
     * @param type $mail
     * @param type $pwd 
     */
    public function  regUser($fname,$mail,$pwd,$sex,$birthday,$ip){
        $db = getDB();
        $set = array();
        $set['Fname'] = $fname;
        $set['Femail'] = $mail;
        $set['Fpwd'] = $pwd;
        $set['Fsex'] = intval($set);
        $set['Fregtime'] = time();
        $set['Fcode'] = md5(sprintf('%d_%s',$set['Fregtime'],$set['Femail']));
        if($birthday){
            $set['Fbirthday'] = intval($birthday);
        }
        $set['Fregip'] = $ip;
        $row = $db->get_row(sprintf("SELECT * FROM t_user_reg WHERE Femail='%s'",$mail)); 
        if($row){
            if($db->updateTable('t_user_reg',$set,'Fuid='.$row['Fuid'])){
                $mail = new Mail;
                $mail->add('url', sprintf('usercenter/user/verifyMail/uid/%s/code/%s',$row['Fuid'],$set['Fcode']));
                $mail->send($mail, sprintf("欢迎 %s 注册",$fname), 'verifyUser');
                return $row['Fuid'];
            }
            return false;
        }else{
            $uid = $db->insertTable('t_user_reg' ,$set);       
            if($uid){
                 $mail = new Mail;
                 $mail->add('url', sprintf('usercenter/user/verifyMail/uid/%s/code/%s',$row['Fuid'],$set['Fcode']));
                 $mail->send($mail, sprintf("欢迎 %s 注册",$fname), 'verifyUser');
                 return $uid;
            }
            return false;
        }        
    }
    /**
     *验证邮箱成功，要将用户注册成为正式的用户
     */
    public function verifyCode($userinfo){
        unset($userinfo['Fcode']);
        unset($userinfo['Fregtime']);
        unset($userinfo['Fuid']);
        $db = getDB();
        return $db->insertTable('t_user',$userinfo);
    }
    /**
     *根据用户id获取用户信息 
     */
    public function getUserById($uid){
        $db = getDB();
        return $db->get_row(sprintf('SELECT Fface AS face,Fname AS name,Femail AS mail,Fsex AS sex,Fbirthday AS birthday,Fpwd FROM t_user WHERE Fuid=%d',$uid));
    }
    /**
     *根据邮箱获取用户信息只在登录的时候使用 
     */
    public function getUserByMail($mail){
        $db = getDB();
        return $db->get_row(sprintf("SELECT Fface AS face,Fname AS name,Femail AS mail,Fsex AS sex,Fbirthday AS birthday,Fpwd FROM t_user WHERE Femail='%s'",$mail));
    }    
}

?>
